<template>
  <div class="dormitoryManager">
    <!-- 位置显示 -->
    <div class='position'>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>系统管理</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/dormitoryManager' }">宿管评分</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 1. 搜索区域 -->
    <div class="header">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="宿管姓名">
          <el-input v-model="formInline.dormitoryManagerName" placeholder="宿管姓名"></el-input>
        </el-form-item>
        <el-form-item v-show="false" label="宿管账户名">
          <el-input v-model="formInline.dormitoryManagerAccount" placeholder="宿管账户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="findAccount()">查询</el-button>
          <template>
            <download-excel class="export-excel-wrapper" :data="tableData" name="score.xls">
              <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
              <el-button type="primary" icon="el-icon-download">导出数据</el-button>
            </download-excel>
          </template>
        </el-form-item>
      </el-form>
    </div>
    <!-- 2. 表格区域展示视图数据 -->
    <div class="wrapper">
      <el-table :data="tableData" border>
        <el-table-column prop="name" label="宿管姓名" width="auto"></el-table-column>
        <el-table-column prop="dmanagerNum" label="宿管编号" width="auto"></el-table-column>
        <el-table-column prop="sex" label="性别" width="auto">
          <template slot-scope="scope">
            <el-tag type="success" effect="dark" v-if="scope.row.sex==1">男</el-tag>
            <el-tag type="danger" effect="dark" v-else>女</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="score" label="宿管得分" width="auto"></el-table-column>
        <el-table-column label="操作" width="270">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="findDate(scope.row)">查看</el-button>
            <el-button type="primary" size="mini" @click="handleEdit( scope.row)" icon="el-icon-edit">
              打分</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 3. 分页 -->
    <MyPagination :total="total" :pageSize="pageSize" @SizeChange="SizeChange" @Pagechange="Pagechange" :currentPage="currentPage" />
    <!-- 查看数据 -->
    <el-dialog title="查看宿管信息" :visible.sync="MessageDalog" width="70%" :before-close="handleClose">
      <el-table :data="rowData">

        <el-table-column prop="name" label="姓名" width="auto"></el-table-column>
        <el-table-column prop="sex" label="性别" width="auto">
          <template slot-scope="scope">
            <el-tag type="success" effect="dark" v-if="scope.row.sex==1">男</el-tag>
            <el-tag type="danger" effect="dark" v-else>女</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="dmanagerNum" label="编号" width="auto"></el-table-column>
        <el-table-column prop="score" label="宿管得分" width="auto"></el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加 -->
    <el-dialog title="宿管评分" :visible.sync="addDialog">
      <el-form :model="addfrom">
         <el-form-item label="姓名" label-width="120px">
          <el-input disabled v-model.trim="addfrom.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-rate v-model="addfrom.score" show-text :texts="texts">
        </el-rate>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click=" addDialog = false">取 消</el-button>
        <el-button type="primary" @click="addScore()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapState } from "vuex"
import { getPageDormitoryManager, ScoreAdd } from '@/api/api'
import MyPagination from "../components/MyPagination.vue"
export default {
  components: {
    MyPagination,
  },
   computed: {
    ...mapState(['role', 'userinfo']),
  },
  data() {
    return {
      //搜索form
      texts: ['2分极差', '4分失望', '6分一般', '8分满意', '10分惊喜'],
      scoreList: [2, 4, 6, 8, 10],
      formInline: {//选择其一进行查询
        dormitoryManagerName: "",
        dormitoryManagerAccount: "",
      },
      tableData: [],//展示页面数据
      total: 5,//总数
      currentPage: 1, //选中的高亮页码
      pageSize: 5,//一页几条数据
      MessageDalog: false,
      rowData: [], //当前行的数据对象
      addDialog: false,
      oldData:[],
      addfrom: {
        name:'',
        score: null,
      },
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      getPageDormitoryManager({ pageNum: this.currentPage, pageSize: this.pageSize, account: this.formInline.dormitoryManagerAccount, name: this.formInline.dormitoryManagerName }).then((result) => {
        console.log(result);
        if (result.status == 200) {
          this.tableData = result.data.list
          this.total = result.data.total
          this.pageSize = result.data.pageSize
          this.currentPage = result.data.pageNum
        } else {
          this.$message.error(result.msg)
        }
      })
    },
    findAccount() {//俩个输入框选其一进行查询
      if ((this.formInline.dormitoryManagerAccount != '' && this.formInline.dormitoryManagerName == '') || (this.formInline.dormitoryManagerAccount == '' && this.formInline.dormitoryManagerName != '')) {
        getPageDormitoryManager({ pageNum: this.currentPage, pageSize: this.pageSize, account: this.formInline.dormitoryManagerAccount, name: this.formInline.dormitoryManagerName }).then((result) => {
          console.log(result);
          if (result.status == 200) {
            this.$message.success('查询成功共' + result.data.total + '条数据！')
            this.tableData = result.data.list
            this.total = result.data.total
            this.pageSize = result.data.pageSize
            this.currentPage = result.data.pageNum
          } else {
            this.$message.error(result.msg)
          }
        })
        this.formInline.dormitoryManagerAccount = ''
        this.formInline.dormitoryManagerName == ''
      } else {
        return this.$message.error('必须任选一个选项进行查询')
      }
    },
    Pagechange(pageNum) {//改变页码
      console.log(pageNum);
      this.currentPage = pageNum
      this.loadData()
    },
    SizeChange(pageSize) {//改变一页有几条
      console.log(pageSize);
      this.pageSize = pageSize
      this.loadData()
    },
    findDate(row) {//打开每行数据dialog
      console.log(row);
      this.rowData.push(row)//添加一行数据到rowdata变量
      this.MessageDalog = true//显示对话框
    },
    handleClose() {//关闭对话框
      this.MessageDalog = false
      this.rowData = []//清空数据
    },
    handleEdit(data) {//信息放到表单中
      this.addDialog = true
      this.addfrom.name = data.name
      this.oldData = data
    },
    addScore() {//打分
    if(this.role!=3) return this.$message.error('只有学生用户才能给宿管打分哦！')
    if(this.addfrom.score == 0 || this.addfrom.score==null ) return this.$message.error('暂未打分，请打分后再次确认!')
      ScoreAdd({sid:this.userinfo.sid,mid:this.oldData.id,score:this.addfrom.score*2}).then(res=>{
          console.log(res);
          if(res.status ==200){
            this.$message.success(res.msg)
            this.loadData()
            this.addDialog = false
          }else{
            this.$message.error(res.msg)
          }
      })
    }
  }
}
</script>

<style lang='less' scoped>
.position {
  // background: #fff;
  padding: 10px;
  // border: 1px solid #eee;
  margin-bottom: 10px;
}
.header {
  display: flex;
  background: #fff;
  padding: 10px;
  border: 1px solid #eee;
  button {
    margin-left: 20px;
  }
  .el-form-item {
    margin-bottom: 0;
  }
}
.wrapper {
  margin: 10px 0;
}
.export-excel-wrapper {
  padding: 0;
  margin: 0;
  float: right;
}
</style>